package com.caiyouxi.finance.service;

import com.caiyouxi.common.Constant;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.finance.mapper.FinanceTradeReceivableMapper;
import com.caiyouxi.finance.model.FinanceTradeReceivable;
import com.caiyouxi.util.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;


/**
 * 应收应付流水记录
 * chensong
 */
@Service
public class FinanceTradeReceivableService extends BaseService {
    
    @Autowired
    private FinanceTradeReceivableMapper financeTradeReceivableMapper;

    public FinanceTradeReceivable add_finance_trade_receivable(BigInteger ftdId,String fundType,Integer totalAmount,Integer sell_user_id,Integer buy_user_id){
        //创建应收应付记录
        FinanceTradeReceivable ftr = new FinanceTradeReceivable();
        ftr.setFinance_trade_details_id(ftdId);
        ftr.setId(new BigInteger(NumUtil.generate_number(4)));
        ftr.setFund_type(fundType);     //费用类型
        ftr.setStatus(Constant.FINANCE_STATUS_PAYABLE);//应付的（但是未收）

        ftr.setFinished_amount(0);//后面更新的时候更新

        ftr.setTotal_amount(totalAmount);
        ftr.setPayee(sell_user_id);//收款方编号
        ftr.setPayer(buy_user_id);//付款方编号
        ftr.setCreate_time(new Date());
        ftr.setUpdate_time(new Date());
        financeTradeReceivableMapper.add_finance_trade_receivable(ftr);
        return ftr;
    }

    /**
     * 查询
     * @param detail_id
     * @return
     */
    public List<FinanceTradeReceivable> select_finance_trade_receivable_by_finance_trade_details_id(BigInteger detail_id){
        return financeTradeReceivableMapper.select_finance_trade_receivable_by_finance_trade_details_id(detail_id);
    }

    /**
     * 更新应收应付流水记录信息状态
     */
    public void update_finance_trade_receivable(FinanceTradeReceivable ftr){
        //更新应收应付记录状态
        ftr.setFinished_amount(ftr.getTotal_amount());
        ftr.setUpdate_time(new Date());
        financeTradeReceivableMapper.update_finance_trade_receivable(ftr);
    }
}
